package com.oracle.zebrafreelytraveling.ui.fragment;

import android.app.FragmentManager;
import android.media.Image;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Scene;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.services.busline.BusStationItem;
import com.amap.api.services.route.BusPath;
import com.amap.api.services.route.BusStep;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.Path;
import com.amap.api.services.route.RailwayStationItem;
import com.amap.api.services.route.RidePath;
import com.amap.api.services.route.RideStep;
import com.amap.api.services.route.WalkPath;
import com.amap.api.services.route.WalkStep;
import com.iflytek.cloud.thirdparty.V;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.map.MapUtil;
import com.oracle.zebrafreelytraveling.map.SchemeBusStep;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.CommonAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;

import org.litepal.crud.DataSupport;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yydcnjjw on 2017/8/8.
 */

public class NaviSlideDialogFragment extends SlideBottomDialogFragment {


    public static final String DATA = "com.oracle.zebrafreelytraveling.ui.fragment.NaviSlideDialogFragment.data";

    private RecyclerView mNaviInfoRecycle;
    private CommonAdapter<Object> mNaviInfoAdapter;
    private List<Object> mNaviInfoList;

    private DrivePath mDrivePath;
    private WalkPath mWalkPath;
    private RidePath mRidePath;
    private BusPath mBusPath;

    boolean arrowExpend = false;

    public static <T extends Path> NaviSlideDialogFragment newInstance(Path path) {
        NaviSlideDialogFragment f = new NaviSlideDialogFragment();
        Bundle args = new Bundle();
        args.putParcelable(DATA, path);
        f.setArguments(args);
        return f;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        getBundleData();
        initView();
        return mView;
    }

    private void getBundleData() {
        Bundle args = getArguments();
        Path path = args.getParcelable(DATA);
        mNaviInfoList = new ArrayList<>();
        mNaviInfoList.add(new Object());
        if (path instanceof DrivePath) {
            mDrivePath = (DrivePath) path;
            mNaviInfoList.addAll(mDrivePath.getSteps());
        } else if (path instanceof WalkPath) {
            mWalkPath = (WalkPath) path;
            mNaviInfoList.addAll(mWalkPath.getSteps());
        } else if (path instanceof RidePath) {
            mRidePath = (RidePath) path;
            mNaviInfoList.addAll(mRidePath.getSteps());
        }
        mNaviInfoList.add(new Object());
        if (path instanceof BusPath) {
            mNaviInfoList.clear();
            mBusPath = (BusPath) path;
            SchemeBusStep start = new SchemeBusStep(null);
            start.setStart(true);
            mNaviInfoList.add(start);
            for (BusStep busStep : mBusPath.getSteps()) {
                if (busStep.getWalk() != null && busStep.getWalk().getDistance() > 0) {
                    SchemeBusStep walk = new SchemeBusStep(busStep);
                    walk.setWalk(true);
                    mNaviInfoList.add(walk);
                }
                if (busStep.getBusLine() != null) {
                    SchemeBusStep bus = new SchemeBusStep(busStep);
                    bus.setBus(true);
                    mNaviInfoList.add(bus);
                }
                if (busStep.getRailway() != null) {
                    SchemeBusStep railway = new SchemeBusStep(busStep);
                    railway.setRailway(true);
                    mNaviInfoList.add(railway);
                }

                if (busStep.getTaxi() != null) {
                    SchemeBusStep taxi = new SchemeBusStep(busStep);
                    taxi.setTaxi(true);
                    mNaviInfoList.add(taxi);
                }
            }
            SchemeBusStep end = new SchemeBusStep(null);
            end.setEnd(true);
            mNaviInfoList.add(end);
        }
    }

    private void initView() {
        mNaviInfoRecycle = (RecyclerView) mView.findViewById(R.id.navi_info_list);
        mNaviInfoRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        mNaviInfoAdapter = new CommonAdapter<Object>(getActivity(), R.layout.item_navi_segment, mNaviInfoList) {
            @Override
            protected void convert(ViewHolder holder, Object o, int position) {
                ImageView naviDirIcon = holder.getView(R.id.navi_dir_icon);
                TextView naviLineName = holder.getView(R.id.navi_line_name);
                ImageView naviDirIconUp = holder.getView(R.id.navi_dir_icon_up);
                ImageView naviDirIconDown = holder.getView(R.id.navi_dir_icon_down);
                ImageView naviSplitLine = holder.getView(R.id.navi_seg_split_line);
                RelativeLayout parent = holder.getView(R.id.bus_item);
                TextView busStationNum = holder.getView(R.id.bus_station_num);
                ImageView busExpandImage = holder.getView(R.id.bus_expand_image);

                if (o instanceof SchemeBusStep) {
                    if (position == 0) {
                        naviDirIcon.setImageResource(R.drawable.dir_start);
                        naviLineName.setText("出发");
                        naviDirIconUp.setVisibility(View.INVISIBLE);
                        naviDirIconDown.setVisibility(View.VISIBLE);
                        naviSplitLine.setVisibility(View.GONE);
                        busStationNum.setVisibility(View.GONE);
                        busExpandImage.setVisibility(View.GONE);

                    } else if (position == mNaviInfoList.size() - 1) {
                        naviDirIcon.setImageResource(R.drawable.dir_end);
                        naviLineName.setText("到达终点");
                        naviDirIconUp.setVisibility(View.VISIBLE);
                        naviDirIconDown.setVisibility(View.INVISIBLE);
                        busStationNum.setVisibility(View.INVISIBLE);
                        busExpandImage.setVisibility(View.INVISIBLE);
                    } else {
                        SchemeBusStep step = (SchemeBusStep) o;
                        if (step.isWalk() && step.getWalk() != null && step.getWalk().getDistance() > 0) {
                            naviDirIcon.setImageResource(R.drawable.dir13);
                            naviDirIconUp.setVisibility(View.VISIBLE);
                            naviDirIconDown.setVisibility(View.VISIBLE);
                            naviLineName.setText("步行"
                                    + (int) step.getWalk().getDistance() + "米");
                            busStationNum.setVisibility(View.GONE);
                            busExpandImage.setVisibility(View.GONE);

                        }else if (step.isBus() && step.getBusLines().size() > 0) {
                            naviDirIcon.setImageResource(R.drawable.dir14);
                            naviDirIconUp.setVisibility(View.VISIBLE);
                            naviDirIconDown.setVisibility(View.VISIBLE);
                            naviLineName.setText(step.getBusLines().get(0).getBusLineName());
                            busStationNum.setVisibility(View.VISIBLE);
                            busStationNum
                                    .setText((step.getBusLines().get(0).getPassStationNum() + 1) + "站");
                            busExpandImage.setVisibility(View.VISIBLE);
                            ArrowClick arrowClick = new ArrowClick(holder, step);
                            parent.setTag(position);
                            parent.setOnClickListener(arrowClick);
                        } else if (step.isRailway() && step.getRailway() != null) {
                            naviDirIcon.setImageResource(R.drawable.dir16);
                            naviDirIconUp.setVisibility(View.VISIBLE);
                            naviDirIconDown.setVisibility(View.VISIBLE);
                            naviLineName.setText(step.getRailway().getName());
                            busStationNum.setVisibility(View.VISIBLE);
                            busStationNum
                                    .setText((step.getRailway().getViastops().size() + 1) + "站");
                            busExpandImage.setVisibility(View.VISIBLE);
                            ArrowClick arrowClick = new ArrowClick(holder, step);
                            parent.setTag(position);
                            parent.setOnClickListener(arrowClick);
                        } else if (step.isTaxi() && step.getTaxi() != null) {
                            naviDirIcon.setImageResource(R.drawable.dir14);
                            naviDirIconUp.setVisibility(View.VISIBLE);
                            naviDirIconDown.setVisibility(View.VISIBLE);
                            naviLineName.setText("打车到终点");
                            busStationNum.setVisibility(View.GONE);
                            busExpandImage.setVisibility(View.GONE);
                        }
                    }
                } else {
                    if (position == 0) {
                        naviDirIcon.setImageResource(R.drawable.dir_start);
                        naviLineName.setText("出发");
                        naviDirIconUp.setVisibility(View.GONE);
                        naviDirIconDown.setVisibility(View.VISIBLE);
                        naviSplitLine.setVisibility(View.GONE);
                    } else if (position == mNaviInfoList.size() - 1) {
                        naviDirIcon.setImageResource(R.drawable.dir_end);
                        naviLineName.setText("到达终点");
                        naviDirIconUp.setVisibility(View.VISIBLE);
                        naviDirIconDown.setVisibility(View.GONE);
                        naviSplitLine.setVisibility(View.VISIBLE);
                    } else {
                        int resID = 0;
                        String naviInstruction = null;
                        if (o instanceof DriveStep) {
                            Log.d("object", "DrivePath");
                            DriveStep driveStep = (DriveStep) o;
                            String actionName = driveStep.getAction();
                            resID = MapUtil.getDriveActionID(actionName);
                            naviInstruction = driveStep.getInstruction();
                        } else if (o instanceof WalkStep) {
                            Log.d("object", "WalkStep");
                            WalkStep walkStep = (WalkStep) o;
                            String actionName = walkStep.getAction();
                            resID = MapUtil.getDriveActionID(actionName);
                            naviInstruction = walkStep.getInstruction();
                        } else if (o instanceof RideStep) {
                            Log.d("object", "RideStep");
                            RideStep rideStep = (RideStep) o;
                            String actionName = rideStep.getAction();
                            resID = MapUtil.getDriveActionID(actionName);
                            naviInstruction = rideStep.getInstruction();
                        }
                        naviDirIcon.setImageResource(resID);
                        naviLineName.setText(naviInstruction);
                        naviDirIconUp.setVisibility(View.VISIBLE);
                        naviDirIconDown.setVisibility(View.VISIBLE);
                        naviSplitLine.setVisibility(View.VISIBLE);
                    }
                }
            }
        };
        mNaviInfoRecycle.setAdapter(mNaviInfoAdapter);
    }

    public void showDialogFragment(FragmentManager fragmentManager) {
        show(fragmentManager, "NAVI_SLIDE_DIALOGFRAGMENT");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dialog_navi_slide;
    }


    private class ArrowClick implements View.OnClickListener {
        private ViewHolder mHolder;
        private SchemeBusStep mItem;
        private ImageView busExpandImage;
        private LinearLayout expandContent;

        public ArrowClick(final ViewHolder holder, final SchemeBusStep item) {
            mHolder = holder;
            mItem = item;
        }

        @Override
        public void onClick(View v) {
            int position = Integer.parseInt(String.valueOf(v.getTag()));
            mItem = (SchemeBusStep) mNaviInfoList.get(position);
            busExpandImage = mHolder.getView(R.id.bus_expand_image);
            expandContent = mHolder.getView(R.id.expand_content);
            if (mItem.isBus()) {
                if (!arrowExpend) {
                    arrowExpend = true;
                    busExpandImage.setImageResource(R.drawable.up);
                    addBusStation(mItem.getBusLine().getDepartureBusStation());
                    for (BusStationItem station : mItem.getBusLine().getPassStations()) {
                        addBusStation(station);
                    }
                    addBusStation(mItem.getBusLine().getArrivalBusStation());

                } else {
                    arrowExpend = false;
                    busExpandImage
                            .setImageResource(R.drawable.down);
                    expandContent.removeAllViews();
                }
            } else if (mItem.isRailway()) {
                if (!arrowExpend) {
                    arrowExpend = true;
                    busExpandImage
                            .setImageResource(R.drawable.up);
                    addRailwayStation(mItem.getRailway().getDeparturestop());
                    for (RailwayStationItem station : mItem.getRailway().getViastops()) {
                        addRailwayStation(station);
                    }
                    addRailwayStation(mItem.getRailway().getArrivalstop());

                } else {
                    arrowExpend = false;
                    busExpandImage
                            .setImageResource(R.drawable.down);
                    expandContent.removeAllViews();
                }
            }


        }

        private void addBusStation(BusStationItem station) {
            LinearLayout ll = (LinearLayout) View.inflate(getActivity(),
                    R.layout.item_bus_segment_ex, null);
            TextView tv = (TextView) ll
                    .findViewById(R.id.bus_line_station_name);
            tv.setText(station.getBusStationName());
            expandContent.addView(ll);
        }

        private void addRailwayStation(RailwayStationItem station) {
            LinearLayout ll = (LinearLayout) View.inflate(getActivity(),
                    R.layout.item_bus_segment_ex, null);
            TextView tv = (TextView) ll
                    .findViewById(R.id.bus_line_station_name);
            tv.setText(station.getName()+ " "+getRailwayTime(station.getTime()));
            expandContent.addView(ll);
        }
    }
    public static String getRailwayTime(String time) {
        return time.substring(0, 2) + ":" + time.substring(2, time.length());
    }
}
